package com.calvin.lingting.actions;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.*;
import android.widget.AbsListView.OnScrollListener;
import com.calvin.lib.EventDispatch.CListen;
import com.calvin.lib.annotation.Notice;
import com.calvin.lib.annotation.ViewInject;
import com.calvin.lib.json.JSONObject;
import com.calvin.lib.notification.NotificationCenter;
import com.calvin.lib.utils.CUtils;
import com.calvin.lingting.MainActivity;
import com.calvin.lingting.MediaPlayService;
import com.calvin.lingting.R;
import com.calvin.lingting.adapter.ChapterListViewAdapter;
import com.calvin.lingting.config.LrtsConfig;
import com.calvin.lingting.config.NTConfig;
import com.calvin.lingting.service.SerializationCenter;
import com.calvin.lingting.utils.Lrts;

public class DetailAction {
    JSONObject mBookInfo;

    ListView mListView;

    boolean mIsLoad = true;

    public DetailAction(JSONObject bookInfo) {
        mBookInfo = bookInfo;
    }

    @ViewInject(isTag = true, tag = "nav_left_button")
    public void onNavLeftClick(View view) {
        NotificationCenter.shared().removeNotice(NTConfig.NT_DS_CHARPTER);
        NotificationCenter.shared().postNotice(NTConfig.NT_NAV_BACK);
    }

    @ViewInject(isTag = true, tag = "nav_right_button")
    public void onNavRightClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.sharedCurrent());
        builder.setTitle(R.string.s_app_name);
        builder.setItems(R.array.a_detail_menu, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                System.out.printf(which + "");
                switch (which) {
                    case 0:
                        SerializationCenter.shared().addLikeBookList(mBookInfo);
                        break;
                    case 1:
                        break;
                }
            }
        });
        builder.create().show();
    }

    @ViewInject(isTag = true, tag = {"nav_title", "nav_right_button", "book_photo", "book_desc", "detail_list_view"}, listen = CListen.INIT)
    public void onInit(View view) {
        if ("nav_title".equals(view.getTag())) {
            CUtils.cast(view, TextView.class).setText(mBookInfo.getStringValue(LrtsConfig.K_NAME));
            NotificationCenter.shared().scanNotice(this);
            Lrts.shared().getChapter(mBookInfo, 1);
        } else if ("book_photo".equals(view.getTag())) {
            Lrts.shared().setBookPhoto((ImageView) view, mBookInfo.getStringValue(LrtsConfig.K_PHOTO));
        } else if ("book_desc".equals(view.getTag())) {
            CUtils.cast(view, TextView.class).setText(mBookInfo.getStringValue(LrtsConfig.K_DESC));
        } else if ("nav_right_button".equals(view.getTag())) {
            view.setBackgroundResource(R.drawable.iconfont_menu);
        } else if ("detail_list_view".equals(view.getTag())) {
            mListView = (ListView) view;
            mListView.setAdapter(new ChapterListViewAdapter(MainActivity.sharedCurrent(), mBookInfo));
            mListView.setOnScrollListener(new ScrollListener());
            if (mBookInfo.containsKey(LrtsConfig.K_CUR_CHAPTER)) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.sharedCurrent());
                builder.setTitle(R.string.s_app_name);
                builder.setMessage(R.string.s_resum_play);
                builder.setPositiveButton(R.string.s_confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int index = mBookInfo.getIntValue(LrtsConfig.K_CUR_CHAPTER);
                        int pos = mBookInfo.getIntValue(LrtsConfig.K_CUR_POS);
                        NotificationCenter.shared().postNotice(NTConfig.NT_MD_PLAY, mBookInfo, index, pos);
                    }
                });
                builder.setNegativeButton(R.string.s_cancel, null);
                builder.create().show();
            }
        }
    }

    @ViewInject(isTag = true, tag = "detail_list_view", listen = CListen.ITEMCLICK)
    public void onListViewItemClick(AdapterView<?> parent, final View view, final int position, long id) {
        if (MediaPlayService.shared().isPlay()) {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.sharedCurrent());
            builder.setTitle(R.string.s_app_name);
            builder.setMessage(R.string.s_switch_media);
            builder.setPositiveButton(R.string.s_switch, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    NotificationCenter.shared().postNotice(NTConfig.NT_MD_PLAY, mBookInfo, position + 1);
                }
            });
            builder.setNegativeButton(R.string.s_cancel, null);
            builder.create().show();
        } else {
            NotificationCenter.shared().postNotice(NTConfig.NT_MD_PLAY, mBookInfo, position + 1);
        }
    }

    @Notice(NTConfig.NT_DS_CHARPTER)
    public void onChartperNotice(Object... argv) {
        JSONObject newJson = (JSONObject) argv[0];
        if (newJson.getJSONObject(LrtsConfig.K_CHAPTER_LIST).size() > 0) {
            if (!mBookInfo.containsKey(LrtsConfig.K_CHAPTER_LIST)) {
                mBookInfo.put(LrtsConfig.K_CHAPTER_LIST, newJson.getJSONObject(LrtsConfig.K_CHAPTER_LIST));
            } else {
                mBookInfo.getJSONObject(LrtsConfig.K_CHAPTER_LIST).putAll(newJson.getJSONObject(LrtsConfig.K_CHAPTER_LIST));
            }
            if (!mBookInfo.containsKey(LrtsConfig.K_CHAPTER)) {
                newJson.remove(LrtsConfig.K_CHAPTER_LIST);
                mBookInfo.putAll(newJson);
            }
            ChapterListViewAdapter adapter = (ChapterListViewAdapter) mListView.getAdapter();
            adapter.setBookInfo(mBookInfo);
            adapter.notifyDataSetChanged();
        }
        mIsLoad = mBookInfo.getIntValue(LrtsConfig.K_CHAPTER) == mBookInfo.getJSONObject(LrtsConfig.K_CHAPTER_LIST).size();
    }


    class ScrollListener implements OnScrollListener {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (firstVisibleItem + visibleItemCount == totalItemCount && !mIsLoad) {
                mIsLoad = true;
                Lrts.shared().getChapter(mBookInfo, mBookInfo.getJSONObject(LrtsConfig.K_CHAPTER_LIST).size() + 1);
            }
        }

    }

}
